<?php if ( ! defined('WDK_PATH')) exit('No direct script access allowed');
require_once BASE_PATH.'app/controller.php';
class Search extends Controller
{
	function __construct()
	{
		parent::__construct();
		$this->_init();
	}
	
	protected function _init() 
	{	
	}
	
	public function index()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$search			= Wdk::get('Search');
		$keyword		= (string) post('keyword', '');
		$assign_data	= array('search'=>array('keyword'=>$keyword));
		/****************************************************************************************************************************/
		try 
		{
			if(strlen(trim($keyword)) < 2)
			{
				parent::_script(array("alert('검색어는 2자이상이여야 합니다')", "history.back();"));
			}
			$search->search($keyword);
			$assign_data['json'] = $this->json(true);
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_view($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function json($return=false)
	{
		/*****************************************************************************************************************************
		 * POST PARAMS..
		*****************************************************************************************************************************/
		$page		= (int)		post('page', 1);
		$keyword	= (string)	post('keyword', '');
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$listsize	= 15;
		$pagesize	= 10;
		$lm = Wdk::get('PostManager');
		$lists = array();
		$where = array("`p`.`bbs_post_title` LIKE '%{$keyword}%'");
		$assign_data = array('listsize'=>$listsize,'pagesize'=>$pagesize);
		/****************************************************************************************************************************/
		try
		{
			$lm['listsize'] = $listsize;
			$lm['page'] = $page;
			$lm['where'] = $where;
			$lm->load();
			foreach($lm['lists'] as $idx=>$list)
			{
				$data = array(	'no'			=>	$lm['total'] - ($page-1)*$listsize - $idx,
								'seq'			=>	$list['bbs_post_seq'],
								'bbs_id'		=>	$list['bbs_id'],
								'bbs_name'		=>	$list['bbs_name'],
								'category_name'	=>	$list['category_name'],
								'user_nick'		=>	$list['user_nick'],
								'title'			=>	$list['bbs_post_title'],
								'secure_flag'	=>	$list['bbs_post_recommend_flag'],
								'comment_num'	=>	$list['comment_num'],
								'insert_time'	=>	$list['bbs_post_insert_time_str']
							);
				array_push($lists, $data);
			}
			$assign_data['total'] = $lm['total'];
			$assign_data['lists'] = $lists;
		}
		catch(NoRecordException $e)
		{
			$assign_data['total'] = 0;
			$assign_data['lists'] = array();
		}
		catch(WException $e)
		{
			$assign_data['error'] = true;
			$assign_data['msg'] = "Error";
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/
		return parent::_json($assign_data, $return);
		/****************************************************************************************************************************/
	}
}